<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<%@ include file="/common/import.jsp" %>
<link href="${ctx}/css/hall.css" rel="stylesheet">
<body>
<%@ include file="/common/halltop.jsp" %>
<div class="wrap" style="height:500px;">
<div style="margin:10px;width:28%;height:300px;float:left;background:#ffaaff">
标的图【${loginUser.userEname}】
</div>
<div style="padding:10px;width:40%;float:left;">
	<table class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>标的名称：</th>
			<td colspan=3>${item.itemName }</td>
		</tr>
		<tr>
			<th>允许修改报价次数：</th>
			<td>${item.allowUpdateTimes }</td>
			<th>已修改报价次数：</th>
			<td><span id="update_times">0</span></td>
		</tr>
		<tr>
			<th>起始价格：</th>
			<td>${item.firstValuePre }</td>
			<th>加价幅度：</th>
			<td><span id="span_increment">${item.incrementValue }</span></td>
		</tr>
		
		<tr>
			<th>所在期：</th>
			<td><font id="itemStatus"></font></td>
			<th>剩余时间：</th>
			<td><font id="spareTime"></font></td>
		</tr>
	</table>	
	<center id="baojiaBtn" style="margin:10px;">
		<div id="status_BC" style="display:none;">
			<div id="div_btn_list">
			<font onclick="dec()">-</font><input type=text id="my_price" readonly="readonly" value="${item.firstValuePre }"><font onclick="add()">+</font>
			<a class="easyui-linkbutton" iconCls="icon-ok" onclick="baojia(5);">报价</a>
			</div>
		</div>
		<div id="status_E" style="display:none;">
				<center id="status_E_1" style="margin:10px;">
							<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="xingquan('T');">我要行权</a> | 
							<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="xingquan('F');">放弃行权</a>
				</center>
				<center id="status_E_2" style="margin:10px;">行权人正在行权过程。。。</center>
		</div>
		<div id="status_Z" style="display:none;">
		    正在处理报价结果
		</div>
		<div id="status_9" style="display:none;">
				<div id="status_9_1">恭喜你！您已是最高报价人</div>
				<div id="status_9_2">报价已结束！</div>
		</div>
	</center>
	</div>
	<div style="padding:2px;width:25%;height:300px;">
	<table id="my_table" class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>报价时间</th><td id="myBaojiaTime"></td>
		</tr>
		<tr>
			<th>报价价格</th><td id="myBaojia"></td>
		</tr>	
	</table>
	</div>
	
	<div id="info_table_div" style="padding:2px;width:25%;height:300px;float:right;display: none;">
	<table id="info_table" class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>报价人</th>
			<th>报价价格</th>	
			<th>报价时间</th>
		</tr>
	</table>
	<script type="text/template" id="info_html">
	<? for ( var i = 0; i < rows.length; i++) {?>
		<tr>
			<td><?=rows[i].userName?></td>
			<td><?=rows[i].bidValue?></td>
			<td><?=rows[i].bidTime?></td>
		</tr>
	<?}?>  		
	</script>
	</div>
</div>
</div>
<%@ include file="/common/hallbottom.jsp" %>
</body>
</html>
<script>
var buttonNum = 5;
var itemId= "${item.itemId}";
var decimalCount = "${item.decimalCount}";
///如果lastValue为空取firstValue;
var lastValue = "${price.firstValue}";
var increment="${item.incrementValue}";
var loginUser = "${loginUser.userEname}";//当前登录人
var updateTimes="${item.allowUpdateTimes}";
var lastUser = "";//最高报价人
var count=0;
function add(){
	count++;
	$("#my_price").val(parseFloat(count* increment) + parseFloat(lastValue));
	
}

function dec(){
	count--;
	if(count<0)
		count=0;
	$("#my_price").val(parseFloat(count* increment) + parseFloat(lastValue));
}

function bidGet(){
	var bidGetUrl = ctx+spaceDomain+'hall/bidGet';
	var bidNum=$("#bid_num").html();
	$.ajax({
		   type:'get',
		   url:bidGetUrl,
		   data:{itemId:itemId},
		   dataType:'json',
		   async:false,
		   success: function(myData){
			    $("#spareTime").html(spareTxt(myData.spareTime));
				   if("9"==myData.itemStatus){
					   setBaoJiaInfo(myData);
					   $("#info_table_div").show();
					   $("#div_btn_list").html("报价已结束");
					   $("#div_btn_list").show();
					   lastValue = myData.lastValue;
					   lastUser = myData.lastUser;
				   }else{
					   if(myData.updateTimes<updateTimes){
						   $("#div_btn_list").show();
					   }else{
						   if(myData.list[0]){
						   $("#div_btn_list").html("您已报过价了，不能再报价");
						   }
						   $("#div_btn_list").show();
					   }
					   $("#update_times").html(myData.updateTimes);
					   if(myData.list[0]){//加载报价记录
							$("#myBaojia").html(myData.list[0].bidValue);
							$("#myBaojiaTime").html(myData.list[0].bidTime);
					   }
				   }
				   setStatus(myData.itemStatus);
		  }
	});	
}

//控制显示区域
var itemStatusOld ="";
function setStatus(status){
	$("#itemStatus").html(itemStatusTxt(status));
	if(itemStatusOld!=status){
		//默认全部隐藏
		$("#status_BC").hide();
		$("#status_D").hide();
		$("#status_E").hide();
		$("#status_9").hide();
		
		//控制每个显示区域里的内容
		if("B"==status){
			$("#status_BC").show();
			
		}else if ("C"==status){
			$("#status_BC").show();
			
		}else if ("D"==status){
			$("#status_D").show();
			if(loginUser==lastUser){
				$("#status_D_1").show();
				$("#status_D_2").hide();
			}else{
				$("#status_D_2").show();
				$("#status_D_1").hide();
			}
		}else if ("E"==status){
			$("#status_E").show();
			if(myLevel=="9"){
				$("#status_E_1").show();
				$("#status_E_2").hide();
			}else{
				$("#status_E_1").hide();
				$("#status_E_2").show();
			}
		}else if ("Z"==status ){
			$("#status_Z").show();
	    }else if ("9"==status ){
			 clearInterval(interv);
			 $("#status_Z").hide();
			$("#status_9").show();
			$("#spareTime").html("-");
			if(loginUser==lastUser){
				$("#status_9_1").show();
				$("#status_9_2").hide();
			}else{
				$("#status_9_2").show();
				$("#status_9_1").hide();
			}
		}
		
	}
	itemStatusOld=status;
}

//加载报价记录
function setBaoJiaInfo(myData){
	deleteAllRow("info_table");
	var myDataBaoJia = new Object();
	myDataBaoJia.rows = myData.list;
	var htmlData = baidu.template('info_html',eval(myDataBaoJia));
	if(htmlData){
		$("#info_table").append(htmlData);
	}
}

//正常报价
function baojia(n){
	//alert(n);
	var myPrice = $("#my_price").val();
		myPrice = formatNumber(myPrice,decimalCount,0);
	if(confirm("你确实要报价"+myPrice+"吗?")){
		var bidSendUrl = ctx+spaceDomain+'hall/bidSend';
		$.ajax({
			   type:'get',
			   url:bidSendUrl,
			   data:{itemId:itemId,myPrice:myPrice},
			   dataType:'json',
			   success: function(myData){
					if(myData.flag=="T"){
						alert("报价成功");
						$("#btnAllowFirst").hide();
					}else{
						alert(myData.msg);
					}
			  }
		});	
	}
}
//行权
function xingquan(flag){
	if("T"==flag){
		if(confirm("你确实要行权吗?")){
			var bidSendUrl = ctx+spaceDomain+'hall/bidXingquan';
			$.ajax({
				   type:'get',
				   url:bidSendUrl,
				   data:{itemId:itemId,flag:flag},
				   dataType:'json',
				   success: function(myData){
						if(myData.flag=="T"){
							alert("行权成功");
							$("#btnAllowFirst").hide();
						}else{
							alert(myData.msg);
						}
				  }
			});	
		}
	}else{
		
	}
}

//设置报价按钮
function setButton(){
	var btnHtml = "";
	var i=0;
	if("${item.allowFirstValue}"=="F"){
		i=1;
	}
	for(i;i<=buttonNum;i++){
		var btnText = parseFloat(increment*i);
		btnHtml+="<a class='easyui-linkbutton' iconCls='icon-add' onclick=\"baojia("+i+");\">+"+btnText+"</a>";
	}
	$("#div_btn_list").html(btnHtml);
	$("#div_btn_list").hide();
}
var interv;
$(function(){
	//setButton();
	interv = setInterval(bidGet,1000);
});


</script>